<?php
	include ("head.html");
?>
<body>
<?php 	
	$username = "";
	$email = "";
	
	if (isset($_POST['submit'])){
		#form hasn't been submitted
		$username = $_POST['username'];
		$email = $_POST['email'];
		$password = $_POST['password'];	
		$password2 = $_POST['passwordTwo'];
		$output_form = false;
		$errors_array = array();
				
		if (empty($username)){
			$errors_array[] = "You forgot to enter username.";
			$output_form = true;
		}
		
		if (empty($email)){
			$errors_array[] = "You forgot to enter eMail.";
			$output_form = true;
		}
		
		if (empty($password)){
			$errors_array[] = "You forgot to enter password.";
			$output_form = true;
		}
		
		if (empty($password2)){
			$errors_array[] = "You forgot to repeat password.";
			$output_form = true;
		}
		
		if ($password != $password2){
			$errors_array[] = "Passwords are not equal.";
			$output_form = true;
		}
		
		
		if (emailAlreadyRegistrated($email)){
			$errors_array[] = "User with eMail $email exists already.";
			$output_form = true;
		}		
		
		if ($output_form == false){
					mysql_connect("localhost", "web964","pass17") or die ("Connection failed.");
					mysql_select_db("usr_web964_1") or die ("Database does not exist.");					
					
					$result = mysql_query("INSERT INTO users (name,email,password) VALUES ('$username','$email','$password')");
					if ($result) {
   		 				header("Location:index.php?page=start&reg=$username");
					} 					
		}
		
	}else{
		$output_form = true;
	}
	
	if ($output_form == true){
?>
	<div id="wrapper">
		<header></header>
		<div id="content">
			<h1>Welcome to Goals to Success!</h1>
			<p>
				We know, you have goals and that you want to reach them. But you don't have an overview what they are about.
				With this web application you can organize them. You can add new goals and subgoals and then see how far you got with them in the progress bar.
				You can also sort them by deadline or priority for example. It is very easy for you and it's a lot of fun to see your own progress!
			</p>
			<div class="right">
				<h1>Registration</h1>				
					<?php
					if (isset($_POST['submit'])) {
						echo "<div class=\"errors\">";
						echo "<p>";
						foreach ($errors_array as $i => $value) {
							if ($errors_array[$i] != "") {
								echo "$errors_array[$i]<br/>";
							}
						}
						echo "</p>";
						echo "</div>";
					}
					?>
				<form id="register" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
		<label>Name *</label>
		<input name="username" type="text" value="<?php echo $username; ?>"/>
		<br />
		<label>eMail *</label>
		<input name="email" type="email" value="<?php echo $email; ?>"/>
		<br />
		<label>Password *</label>
		<input name="password" type="password"/>
		<br />
		<label>Repeat password *</label>
		<input name="passwordTwo" type="password"/>
		<br />
		<input value="Sign up" type="submit" name="submit">
		</form>
		<p>
			Do you already have an account? Then <a href="index.php">log in</a> and reach your goals!
		</p>
		</div>
		</div>
		<footer></footer>
		</div>
	</body>
	<?php } ?>
</html>

<?php 
	function emailAlreadyRegistrated($email){
		mysql_connect("localhost", "web964","pass17") or die ("Connection failed.");
		mysql_select_db("usr_web964_1") or die ("Database does not exist.");					
					
		$result = mysql_query("SELECT email FROM users WHERE email='$email'" ) or die ("MySQL-Error: " . mysql_error());
		if (mysql_num_rows($result) > 0){
			return true;
		}
		return false;
	}
?>
